package com.hy.demo.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hy.demo.adapter.RcyMenuAdapter;
import com.hy.demo.model.MenuInfo;
import com.hy.demo.ui.MenuActivity;
import com.hy.frame.adapter.IAdapterListener;
import com.hy.frame.recycler.GridItemDecoration;
import com.hy.frame.util.ActUtil;
import com.hy.frame.util.FormatUtil;
import com.hy.frame.util.ResUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class MenuViewUtil {
    public static final String ARG_XML = "arg_xml";
    public static final String ARG_TITLE = "arg_title";
    public static final String KEY_CLS = "tCls";
    public static final String KEY_MENU = "tMenu";
    public static final String KEY_ARGS = "tArgs";

    private Context context;
    private Fragment fragment;
    private RecyclerView rcyList;
    private int menuId;
    private List<MenuInfo> datas;
    private RcyMenuAdapter adapter;
    private IAdapterListener<MenuInfo> listener;

    private MenuViewUtil(RecyclerView rcyList, int menuId, Activity activity) {
        this.context = activity;
        this.rcyList = rcyList;
        this.menuId = menuId;
        init();
    }

    private MenuViewUtil(RecyclerView rcyList, int menuId, Fragment fragment) {
        this.context = fragment.getActivity();
        this.fragment = fragment;
        this.rcyList = rcyList;
        this.menuId = menuId;
        init();
    }

    private void init() {
        rcyList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        if (rcyList.getItemDecorationCount() == 0) {//防止界面恢复 重复添加
            int padding = ResUtil.dip2px(getContext(), 10);
            rcyList.addItemDecoration(new GridItemDecoration(padding).setVerticalDivider(padding).setPadding(padding, padding, padding, padding));
        }
        this.datas = MenuUtil.getMenus(getContext(), menuId);
        this.listener = new IAdapterListener<MenuInfo>() {
            @Override
            public void onViewClick(View view, MenuInfo item, int position) {
                String clsStr = item.getValue(KEY_CLS);
                String menuStr = item.getValue(KEY_MENU);
                if (FormatUtil.isNoEmpty(menuStr)) {
                    int xmlId = getXmlId(menuStr);
                    Bundle bundle = new Bundle();
                    bundle.putInt(ARG_XML, xmlId);
                    bundle.putString(ARG_TITLE, item.getTitle());
                    startAct(MenuActivity.class, bundle);
                    return;
                }
                if (FormatUtil.isEmpty(clsStr)) return;
                String args = item.getValue(KEY_ARGS);
                Bundle bundle = new Bundle();
                if (FormatUtil.isNoEmpty(args)) {
                    JSONArray json;
                    try {
                        json = new JSONArray(args);
                        String type;
                        String key;
                        JSONObject obj;
                        if (json.length() > 0) {
                            for (int i = 0; i < json.length(); i++) {
                                obj = (JSONObject) json.get(0);
                                type = obj.getString("type");
                                key = obj.getString("key");
                                if (type.equalsIgnoreCase("int")) {
                                    bundle.putInt(key, obj.getInt("value"));
                                } else if (type.equalsIgnoreCase("long")) {
                                    bundle.putLong(key, obj.getLong("value"));
                                } else if (type.equalsIgnoreCase("boolean")) {
                                    bundle.putBoolean(key, obj.getBoolean("value"));
                                } else if (type.equalsIgnoreCase("double")) {
                                    bundle.putDouble(key, obj.getDouble("value"));
                                } else {
                                    bundle.putString(key, obj.getString("value"));
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                try {
                    Class<?> cls = Class.forName(clsStr);
                    startAct(cls, bundle);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        adapter = new RcyMenuAdapter(getContext(), datas, this.listener);
        rcyList.setAdapter(adapter);
    }

    private Context getContext() {
        return context;
    }

    private Resources getResources() {
        return getContext().getResources();
    }

    private int getXmlId(String name) {
        return getResources().getIdentifier(name, "menu", getContext().getPackageName());
    }

    private void startAct(Class<?> cls, Bundle bundle) {
        if (fragment != null) {
            ActUtil.startAct(fragment, cls, bundle);
        } else {
            ActUtil.startAct(context, cls, bundle);
        }
    }

    public static MenuViewUtil attach(RecyclerView rcyList, int menuId, Fragment fragment) {
        return new MenuViewUtil(rcyList, menuId, fragment);
    }

    public static MenuViewUtil attach(RecyclerView rcyList, int menuId, Activity activity) {
        return new MenuViewUtil(rcyList, menuId, activity);
    }

    public static void startAct(Fragment fragment, int xmlId, String title) {
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_XML, xmlId);
        bundle.putString(ARG_TITLE, title);
        ActUtil.startAct(fragment, MenuActivity.class, bundle);
    }

    public static void startAct(Activity activity, int xmlId, String title) {
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_XML, xmlId);
        bundle.putString(ARG_TITLE, title);
        ActUtil.startAct(activity, MenuActivity.class, bundle);
    }
}
